package com.example.myapp.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp.ADO.Mini_Adapter;
import com.example.myapp.Listviewloading.MyFooterAdapter;
import com.example.myapp.Listviewloading.MyHeaderAdapter;
import com.example.myapp.Loading.ShapeLoadingDialog;
import com.example.myapp.Model.Article;
import com.example.myapp.Model.User;
import com.example.myapp.R;
import com.sak.ultilviewlib.UltimateRefreshView;
import com.sak.ultilviewlib.interfaces.OnFooterRefreshListener;
import com.sak.ultilviewlib.interfaces.OnHeaderRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Myzhuanfa extends AppCompatActivity {
    private List<Article> data = new ArrayList<>();
    private  List<String> listarr = new ArrayList<>();

    private  ShapeLoadingDialog shapeLoadingDialog;


    private UltimateRefreshView mUltimateRefreshView;
    private int page = 0;
    private RecyclerView recyclerView;
    private Mini_Adapter adapter;



    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1: {
                    queryALLzhuanfa();
                    break;
                }
            }
        }
    };

    public void onBackPressed() {
        //此处写退向后台的处理
        finish();
    }//手机返回键
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);  //隐藏标题栏
        setContentView(R.layout.activity_my_articles);
         TextView myarticles_title=findViewById(R.id.myarticles_title);
        myarticles_title.setText("我的转发");
        recyclerView=findViewById(R.id.mypostslistview);
        ImageView myarticles_back=findViewById(R.id.myarticles_back);
        myarticles_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mUltimateRefreshView = (UltimateRefreshView)findViewById(R.id.refreshView);
        mUltimateRefreshView.setBaseHeaderAdapter(new MyHeaderAdapter(Myzhuanfa.this));


        /**底部上拉加载*/
        mUltimateRefreshView.setBaseFooterAdapter(new MyFooterAdapter(Myzhuanfa.this));

        mUltimateRefreshView.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {
            @Override
            public void onHeaderRefresh(UltimateRefreshView view) {

                mUltimateRefreshView.onHeaderRefreshComplete();
            }
        });

        /**底部上拉加载事件*/
        mUltimateRefreshView.setOnFooterRefreshListener(new OnFooterRefreshListener() {
            @Override
            public void onFooterRefresh(UltimateRefreshView view) {
                page++;
                BmobQuery<Article> query = new BmobQuery<>();
                query.addWhereEqualTo("author",new BmobPointer(BmobUser.getCurrentUser(User.class)));
                //包含作者信息
                query.include("author");
                query.order("-orderTime");
                query.setSkip(page*5); // 忽略前10条数据（即第一页数据结果）
                query.setLimit(5); // 限制最多10条数据结果作为一页
                //包含作者信息
                query.findObjects(new FindListener<Article>() {
                    @Override
                    public void done(List<Article> object, BmobException e) {
                        if (e == null) {
                            if (object.size()>0){
                                for (int i = 0; i < object.size(); i++) {
                                    if (object.get(i).getZhuanfaContent() != null) {
                                        data.add(object.get(i));
                                    }
                                }
                                adapter.notifyDataSetChanged();
                                mUltimateRefreshView.onFooterRefreshComplete();
                            }else {
                                mUltimateRefreshView.onFooterRefreshComplete();
                            }
                        } else {
                            Log.e("aaaa","查询我的动态失败"+ e.getMessage());
                            mUltimateRefreshView.onFooterRefreshComplete();
                        }
                    }

                });



            }
        });


        Message message = new Message();
        message.what = 1;
        handler.sendMessage(message);

    }
    /**
     查询一对多，我的所有动态
     */
    private void queryALLzhuanfa(){
        openLoading();
        if (BmobUser.isLogin()) {
            // 用此方式可以构造一个BmobPointer对象。只需要设置objectId就行
            BmobQuery<Article> query = new BmobQuery<>();
            query.addWhereEqualTo("author",new BmobPointer(BmobUser.getCurrentUser(User.class)));
            //包含作者信息
            query.include("author");
            query.order("-orderTime");
            query.setLimit(5); // 限制最多5条数据结果作为一页
            query.findObjects(new FindListener<Article>() {
                @Override
                public void done(List<Article> object, BmobException e) {
                    if (e == null) {
                        Log.w("aaaa", "我转发页面所有动态"+object.size());
                        if(object.size()>0) {
                            for (int i = 0; i < object.size(); i++) {
                                if (object.get(i).getZhuanfaContent() != null) {
                                    data.add(object.get(i));
                                }
                            }
                            LinearLayoutManager layoutManager = new LinearLayoutManager(Myzhuanfa.this);//正常listview
                            recyclerView.setLayoutManager(layoutManager);
                            recyclerView.setItemViewCacheSize(500);
                            adapter = new Mini_Adapter(Myzhuanfa.this, data);
                            recyclerView.setAdapter(adapter);
                            closeLoading();
                        }

                    } else {
                        Log.e("aaaa","查询我的动态失败"+ e.getMessage());
                    }
                }

            });
        } else {
            Log.w("aaaa", "请先登录！");
        }
    }


    //网络加载
    private void openLoading() {
        shapeLoadingDialog = new ShapeLoadingDialog.Builder(Myzhuanfa.this)
                .loadText("加载中...")
                .build();
        shapeLoadingDialog.show();
    }

    //关闭网络加载
    private void closeLoading() {
        shapeLoadingDialog.dismiss();
    }
}
